package com.eascs.admin.o2o.enterpriseZX.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.back.service.O2oBackService;
import com.eascs.admin.o2o.enterpriseZX.entity.O2oEnterpriseZxEntity;
import com.eascs.admin.o2o.enterpriseZX.service.O2oEnterpriseZxService;
import com.eascs.admin.o2o.enterpriseZX.vo.O2oEnterpriseZxVo;
import com.eascs.admin.o2o.internal.util.BaseUtils;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.common.util.UrlUtil;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.loanstart.controller.LoanStartController;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.datalist.service.O2oDataListService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.site.view.button.ShowButtonFilter;
import com.eascs.tag.MenuTag;
import com.eascs.web.context.RequestContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/enterpriseZx")
public class O2oEnterpriseZxController extends BaseMasterViewController {
	private final Logger logger = LoggerFactory.getLogger(LoanStartController.class);
	
	
	@Reference
	CustomerService customerService;

	@Autowired
	private O2oEnterpriseZxService o2oEnterpriseZxService;
	
	@Autowired
	private O2oDataListService o2oDataListService;
	
	@Reference
	SysOrgService sysOrgService;
	
//	@Override
//	public void preHandle() {
//		//super.preHandle();
//		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "afterLoanManage");
//	}

	@Autowired
	O2oBackService service;
	
	/**
	 * @Title: index
	 * @Description: 企业征信查询-查询客户
	 * @author yy
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年11月8日 下午2:17:31
	 */
	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(String idCard) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "enterpriseAddNewZx");
		if(StringUtil.isNotBlank(idCard)){
		   idCard=idCard.trim();
		}
		ViewContext.put("idCard", idCard);
		logger.info("idCard:", idCard);
		return "enterpriseZx/index";

	}
	
	
	/**
	 * @Title: search
	 * @Description: 根据查询结果跳转到企业征信-编辑企业征信信息 
	 * @author yy
	 * @param idCard
	 * @return
	 * @return String
	 * @throws @date
	 *             2017年7月3日 下午2:17:44
	 */
	@RequestMapping(value = "/search")
	public String search(String idCard) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "apply_search");
		if(StringUtil.isNotBlank(idCard)){
			   idCard=idCard.trim();
		}
		ViewContext.put("idCard", idCard);
		CustomerEntity basic = customerService.queryCustomerEntityByIdCard(idCard);
	
		ViewContext.put("idCard", idCard);
		ViewContext.put("vo", basic);
		return "/enterpriseZx/editEnterprise";

	}
	
	/**
	 * 修改详情页面
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/findO2oEnterpriseZx")
	public String uploadfile(String id, String massege) {
		
		O2oEnterpriseZxEntity o2oEnterpriseZxEntity  = o2oEnterpriseZxService.getEntityById(id);
		
		/**** 查询附件 ****/
		List<O2oDataListEntity> o2oDataList = o2oDataListService.findByEntityList(o2oEnterpriseZxEntity.getId());
		
		ViewContext.put("o2oDataList", o2oDataList);
		ViewContext.put("errorMassege", massege);
		ViewContext.put("entity", o2oEnterpriseZxEntity);
		
		return "/enterpriseZx/uploadfile";
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public String search(O2oEnterpriseZxVo o2oZxVo) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "qyzx_liebiao");
		o2oZxVo.setPageSize(BaseUtils.PAGE_SIZE);
	
		List<User> users=sysOrgService.checkDataPermission(UserHolder.getUser().getNo());
		String no="";
		if (users!=null) {
			for (User user : users) {
				no+=user.getNo()+",";
			}
		}
		if (!"".equals(no)) {
			if (",".equals(no.substring(no.length()-1, no.length()))) {
				no=no.substring(0, no.length()-1);
			}
		}
		logger.info("userno={}", no);
		o2oZxVo.setType(no);
		
		PageResult<Object[]> pageResult = o2oEnterpriseZxService.findByPage(o2oZxVo);
		String[] titles = {"单号", "客户姓名", "营业执照名称", "征信银行", "信贷经理","经办平台", "创建时间", "单据状态"};
		SelectDataOption[] selectDataOptions = {null, null,  new SelectDataOption("ZX_TYPE"), new SelectDataOption("BANKTYPEO2O"),null,null,null,new SelectDataOption("O2O_BILLSTATUS")};
		String pageUrl = UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", "");
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, pageUrl);
		paginationInfo.setSelectDataOptions(selectDataOptions);
		Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/enterpriseZx/list_detail?id=") };
		paginationInfo.setTitleButtons(titleButton);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setFirstRenderColumnIndex(1);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("o2oZxVo", o2oZxVo);
		return "/enterpriseZx/list";
	}
	
	
	/**
	 * 列表详情页
	 * 
	 * @param loanapplyNo
	 * @param massege
	 * @return
	 */
	@RequestMapping(value = "/list_detail")
	public String detail(String id, String massege) {
//		User user = UserHolder.getUser();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "detail_nzx_liebiao");
		O2oEnterpriseZxEntity o2oEnterprise  = o2oEnterpriseZxService.getEntityById(id);
		Button button = ButtonHelper.createAjaxButton("启动流程", "/work/newQYZXstartup?loanapplyNo=" + o2oEnterprise.getId());
		button.setPermissionCode("enterpriseZxStartFlowButton");
		//*****loan-12273 征信查询单据删除功能 start
		AjaxConfirmButton delBtn = new AjaxConfirmButton("删除",  "/renterpriseZx/delete?id=" + o2oEnterprise.getId());
		delBtn.setPermissionCode("enterpriseDeleteNewZx");
		delBtn.setConfirmMessage("删除数据不可恢复，确认删除？");
		List<Button> btnS = new ArrayList<Button>();
		btnS.add(button);
		btnS.add(delBtn);
		//**********end
		ViewContext.put("buttons", btnS);
		String approvUrl = "/wf/processNode/agreeInfo?referId="+o2oEnterprise.getId()+"&amp;processType=E_CREDIT";
		ViewContext.put("errorMassege", massege);
		ViewContext.put("approvUrl", approvUrl);
		ViewContext.put("o2oEnterprise", o2oEnterprise);
		String datalistReadonly = "true";
		ViewContext.put("datalistReadonly", datalistReadonly);
		return "enterpriseZx/list_detail";
	}
	
	@RequestMapping("/zxbgTDView")
	public String zxDTbgView(String loanapplyNo) {
			PageResult<Object[]> paymentsResult = o2oEnterpriseZxService.queryFqZxDTbg(loanapplyNo);
			String[] paymentstitles = { "征信单号","姓名", "营业执照名称", "企业征信报告日期", "征信创建时间","状态", "更新人", "更新时间" };
			PaginationInfo paginationInfo = new PaginationInfo(paymentsResult, paymentstitles);
			SelectDataOption[] selectDataOptions = {null,null,null,null,null,new SelectDataOption("O2O_BILLSTATUS")};
			paginationInfo.setSelectDataOptions(selectDataOptions);
			paginationInfo.setBuildPagingNavigation(false);
			paginationInfo.setFirstRenderColumnIndex(2);
			paginationInfo.setBuildRowNumber(false);
		    Button selectButton=ButtonHelper.createAjaxButton("选择企业征信报告", "/renterpriseZx/loanFqZxbgTD?loanapplyNo="+loanapplyNo+"&IdNum={1}") ;
		    Button createButton=ButtonHelper.createAjaxButton("删除", "/renterpriseZx/zxdelTD?id=") ;
		    createButton.setShowButtonFilter(new ShowButtonFilter() {

				@Override
				public boolean filter(Object param) {
					Object[] params = (Object[]) param;
					if (!StringUtil.isEmpty(params[0])) {
						return true;
					} else
						return false;
				}
			});
			Button[] moreButtons = new Button[] {selectButton,createButton};
			paginationInfo.setMoreButtons(moreButtons);
			Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/enterpriseZx/list_detail?id=") };
			paginationInfo.setTitleButtons(titleButton);
			ViewContext.put("paginationInfo", paginationInfo);
			return "/enterpriseZx/zxbgTDView";
	}
	@RequestMapping("/zxbgTDViewNot")
	public String zxDTbgViewNot(String loanapplyNo) {
			PageResult<Object[]> paymentsResult = o2oEnterpriseZxService.queryFqZxDTbg(loanapplyNo);
			String[] paymentstitles = { "征信单号","姓名", "营业执照名称", "企业征信报告日期", "征信创建时间","状态", "更新人", "更新时间" };
			PaginationInfo paginationInfo = new PaginationInfo(paymentsResult, paymentstitles);
			SelectDataOption[] selectDataOptions = {null,null,null,null,null,new SelectDataOption("O2O_BILLSTATUS")};
			paginationInfo.setSelectDataOptions(selectDataOptions);
			paginationInfo.setBuildPagingNavigation(false);
			paginationInfo.setFirstRenderColumnIndex(2);
			paginationInfo.setBuildRowNumber(false);
			Button[] titleButton = new Button[] { ButtonHelper.createRedirectButton(null, "/enterpriseZx/list_detail?id=") };
			paginationInfo.setTitleButtons(titleButton);
			ViewContext.put("paginationInfo", paginationInfo);
			return "/enterpriseZx/zxbgTDView";
	}
}
